//    Copyright (C) Kherty.  All rights reserved.
using System;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Controls.Primitives;
using System.Windows.Data;
using System.Windows.Input;
using System.Windows.Markup;
using System.Windows.Media;

namespace OpenLS.UI
{
    [ContentProperty("Text")]
    class AccessText : FrameworkElement
    {

        public AccessText()
        {
            Style = DefaultStyle;
            Focusable = false;
            PreviewKeyDown += MyAccessText_PreviewKeyDown;
        }

        static void MyAccessText_PreviewKeyDown(object sender, KeyEventArgs e)
        {
            //\\AccessKeyVisible = (bool)GetValue(ShowKeyboardCuesProperty);
        }

        public string Text
        {
            get { return (string)GetValue(TextProperty); }
            set { SetValue(TextProperty, value); }
        }

        // Using a DependencyProperty as the backing store for Text.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty TextProperty =
            DependencyProperty.Register("Text", typeof(string), typeof(AccessText), new FrameworkPropertyMetadata(string.Empty, FrameworkPropertyMetadataOptions.AffectsRender | FrameworkPropertyMetadataOptions.AffectsMeasure, OnTextChanged));


        private static void OnTextChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            AccessText a = (AccessText)d;
            a.OnTextChanged((string)e.NewValue);
        }
        void OnTextChanged(string text)
        {
            StringBuilder sb = new StringBuilder();
            string accessKey = "";
            for (int i = 0; i < Text.Length - 1; i++)
            {
                char c = Text[i];
                if (c == '_')
                {
                    i++;
                    c = Text[i];
                    accessKey += c;
                }
                sb.Append(c);

            }
            if (text.Length > 0)
                sb.Append(text[text.Length - 1]);
            AccessKey = accessKey;
            VisualText = sb.ToString();
            TextBlock.Text = VisualText;

        }


        public string VisualText
        {
            get { return (string)GetValue(VisualTextProperty); }
            set { SetValue(VisualTextProperty, value); }
        }

        // Using a DependencyProperty as the backing store for VisualText.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty VisualTextProperty =
            DependencyProperty.Register("VisualText", typeof(string), typeof(AccessText), new UIPropertyMetadata(null));



        public string AccessKey
        {
            get { return (string)GetValue(AccessKeyProperty); }
            set { SetValue(AccessKeyProperty, value); }
        }

        // Using a DependencyProperty as the backing store for AccessKey.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty AccessKeyProperty =
            DependencyProperty.Register("AccessKey", typeof(string), typeof(AccessText), new FrameworkPropertyMetadata(null, OnAccessKeyChanged));
        static void OnAccessKeyChanged(DependencyObject obj, DependencyPropertyChangedEventArgs args)
        {
            ((AccessText)obj).RegisterAccessKey((string)args.OldValue, (string)args.NewValue);
        }

        static Style defaultStyle;
        private static Style DefaultStyle
        {
            get
            {
                if (defaultStyle == null)
                {
                    Style style = new Style(typeof(AccessText));
                    Trigger item = new Trigger();
                    DependencyProperty property = ShowKeyboardCuesProperty;
                    item.Property = property;
                    item.Value = true;
                    item.Setters.Add(new Setter(AccessKeyVisibleProperty, true));
                    style.Triggers.Add(item);
                    style.Seal();
                    defaultStyle = style;
                }
                return defaultStyle;
            }
        }



        public bool AccessKeyVisible
        {
            get { return (bool)GetValue(AccessKeyVisibleProperty); }
            set { SetValue(AccessKeyVisibleProperty, value); }
        }

        // Using a DependencyProperty as the backing store for AccessKeyVisible.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty AccessKeyVisibleProperty =
            DependencyProperty.Register("AccessKeyVisible", typeof(bool), typeof(AccessText), new FrameworkPropertyMetadata(false, new PropertyChangedCallback(OnAccessKeyVisibleChanged)));

        static void OnAccessKeyVisibleChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            return;
        }


        public PlacementMode TooltipPlacementMode
        {
            get { return (PlacementMode)GetValue(TooltipPlacementModeProperty); }
            set { SetValue(TooltipPlacementModeProperty, value); }
        }



        public double TooltipVerticalOffset
        {
            get { return (double)GetValue(TooltipVerticalOffsetProperty); }
            set { SetValue(TooltipVerticalOffsetProperty, value); }
        }

        // Using a DependencyProperty as the backing store for TooltipVerticalOffset.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty TooltipVerticalOffsetProperty =
            DependencyProperty.Register("TooltipVerticalOffset", typeof(double), typeof(AccessText), new UIPropertyMetadata(0.0d));



        // Using a DependencyProperty as the backing store for TooltipPlacementMode.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty TooltipPlacementModeProperty =
            DependencyProperty.Register("TooltipPlacementMode", typeof(PlacementMode), typeof(AccessText), new UIPropertyMetadata(PlacementMode.Bottom));


        // Using a DependencyProperty as the backing store for OldToolTyp.  This enables animation, styling, binding, etc...


        public Brush Background
        {
            get { return (Brush)GetValue(BackgroundProperty); }
            set { SetValue(BackgroundProperty, value); }
        }

        // Using a DependencyProperty as the backing store for Background.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty BackgroundProperty =
            DependencyProperty.Register("Background", typeof(Brush), typeof(AccessText), new UIPropertyMetadata(null));


        static DependencyProperty ShowKeyboardCuesProperty
        {
            get
            {
                return (DependencyProperty)typeof(KeyboardNavigation).GetField("ShowKeyboardCuesProperty", System.Reflection.BindingFlags.Static | System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Public).GetValue(null);
            }
        }

        protected override int VisualChildrenCount
        {
            get
            {
                return 1;
            }
        }
        protected override Visual GetVisualChild(int index)
        {
            switch (index)
            {
                case 0:
                    return TextBlock;
            }
            throw new Exception();
        }

        protected sealed override Size MeasureOverride(Size constraint)
        {
            TextBlock.Measure(constraint);
            return TextBlock.DesiredSize;
        }
        protected sealed override Size ArrangeOverride(Size arrangeSize)
        {
            TextBlock.Arrange(new Rect(arrangeSize));
            return arrangeSize;
        }
        private TextBlock _textBlock;

        internal TextBlock TextBlock
        {
            get
            {
                if (_textBlock == null)
                {
                    CreateTextBlock();
                }
                return _textBlock;
            }
        }
        private void RegisterAccessKey(string oldKey, string newKey)
        {
            if (oldKey != null && newKey.Length > 0)
            {
                AccessKeyManager.Unregister(oldKey, this);
            }
            if ((newKey  != null) && (newKey.Length > 0))
            {
                AccessKeyManager.Register(newKey, this);
            }
        }






        private void CreateTextBlock()
        {
            _textBlock = new TextBlock();
            _textBlock.Focusable = false;
            AddVisualChild(_textBlock);
            Binding binding = new Binding("Background");
            binding.Source = this;
            BindingOperations.SetBinding(TextBlock, System.Windows.Controls.TextBlock.BackgroundProperty, binding);
            binding = new Binding("TextEffects");
            binding.Source = this;
            BindingOperations.SetBinding(TextBlock, System.Windows.Controls.TextBlock.TextEffectsProperty, binding);
            binding = new Binding("TextDecorations");
            binding.Source = this;
            BindingOperations.SetBinding(TextBlock, System.Windows.Controls.TextBlock.TextDecorationsProperty, binding);
            binding = new Binding("TextTrimming");
            binding.Source = this;
            BindingOperations.SetBinding(TextBlock, System.Windows.Controls.TextBlock.TextTrimmingProperty, binding);
            binding = new Binding("TextWrapping");
            binding.Source = this;
            BindingOperations.SetBinding(TextBlock, System.Windows.Controls.TextBlock.TextWrappingProperty, binding);
            binding = new Binding("BaselineOffset");
            binding.Source = this;
            BindingOperations.SetBinding(TextBlock, System.Windows.Controls.TextBlock.BaselineOffsetProperty, binding);
        }



    }

}
